package com.rui.study.demo.httpclient;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.executor.ShardingContexts;
import com.dangdang.ddframe.job.lite.api.listener.AbstractDistributeOnceElasticJobListener;
import com.dangdang.ddframe.job.reg.base.RegistryCenter;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: study_20180903
 * @description:
 * @author: Yaowr
 * @create: 2018-10-30 15:52
 **/
@Component
public class MyElasticJob extends AbstractDistributeOnceElasticJobListener implements SimpleJob {

    private RegistryCenter registryCenter;

    public MyElasticJob(RegistryCenter registryCenter) {
        super(100, 100);
        this.registryCenter = registryCenter;
    }

    @Override
    public void execute(ShardingContext context) {

        String threadName = Thread.currentThread().getName();
        String jobName = context.getJobName();
        String taskId = context.getTaskId();
        int shardingItem = context.getShardingItem();

        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

        System.out.println(time + "：[" + threadName + "]---[" + jobName +"]---[" + taskId + "]---[" + shardingItem + "]");
    }


    @Override
    public void doBeforeJobExecutedAtLastStarted(ShardingContexts shardingContexts) {
        System.out.println("Before " + shardingContexts.getJobName());

    }

    @Override
    public void doAfterJobExecutedAtLastCompleted(ShardingContexts shardingContexts) {
        String jobName = shardingContexts.getJobName();
        System.out.println("After " + jobName);
//        registryCenter.remove("/" + jobName);
    }
}
